from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from message import models


class MessageSer(serializers.ModelSerializer):
    ass = serializers.SerializerMethodField(read_only=True)
    def get_ass(self,obj):
        return obj.au.name
    class Meta:
        model = models.Message
        fields = '__all__'

class AuthorSer(serializers.ModelSerializer):
      class Meta:
        model = models.Author
        fields = '__all__'
      def validate_name(self,value):
          if '积云' not in value:
              raise ValidationError('名字必须包含积云')
          menu = models.Author.objects.filter(name=value)
          if menu:
              raise ValidationError('名字重复')
          return value

